package com.rc.saas.tenant.service.discount;

import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.discount.TenantDiscountMemberMapper;
import com.rc.saas.tenant.model.discount.TenantDiscountMember;
import com.rc.saas.tenant.model.discount.TenantDiscountMemberExample;
import com.rc.saas.tenant.model.discount.TenantDiscountMemberStation;
import com.rc.saas.tenant.model.discount.TenantDiscountPolicy;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.service.member.MemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2019/11/25
 */
@Service
public class TenantDiscountMemberServiceImpl implements TenantDiscountMemberService {

    @Autowired
    private TenantDiscountMemberMapper tenantDiscountMemberMapper;

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private TenantDiscountPolicyService tenantDiscountPolicyService;

    @Autowired
    private TenantDiscountMemberStationService tenantDiscountMemberStationService;

    @Override
    public TenantDiscountMemberMapper getMapper() {
        return tenantDiscountMemberMapper;
    }

    @Override
    public TResponse insertDiscountMember(String tenantCode, String memberType, Long discountPolicyId, String stringList) {
        Date date = new Date();
        long time = date.getTime();

        TenantDiscountPolicy tenantDiscountPolicy = tenantDiscountPolicyService.selectByPrimaryKey(discountPolicyId);

        TResponse getOnPolicy = tenantDiscountPolicyService.isUsingPolicy(tenantDiscountPolicy.getElecStationId(), discountPolicyId);
        if (getOnPolicy.getStatus() != 200) {
            //如果存在未开始或者进行中的方案将状态更改为禁止
            //启用状态 0未开始 1进行中 2已结束 3已禁止 默认0
            tenantDiscountPolicy.setStatus("3");
        } else {
            if (tenantDiscountPolicy.getBeginTime().getTime() >= time) {
                tenantDiscountPolicy.setStatus("0");
            } else if (tenantDiscountPolicy.getBeginTime().getTime() <= time && tenantDiscountPolicy.getEndTime().getTime() >= time) {
                tenantDiscountPolicy.setStatus("1");
            } else {
                tenantDiscountPolicy.setStatus("2");
            }

        }

        //todo 先删除原来的关系 根据方案的id
        List<TenantDiscountMember> tenantDiscountMembers = selectMemberByDiscountId(tenantDiscountPolicy.getDiscountPolicyId());
        if (tenantDiscountMembers != null) {
            ArrayList<Long> list = new ArrayList<>();
            for (TenantDiscountMember tenantDiscountMember : tenantDiscountMembers) {
                list.add(tenantDiscountMember.getDiscountMemberId());
            }
            deleteByBatch(list);
        }
        List<TenantDiscountMemberStation> tenantDiscountMemberStations = tenantDiscountMemberStationService.selectMemberStationByDiscountId(tenantDiscountPolicy.getDiscountPolicyId());
        if (tenantDiscountMemberStations != null) {
            ArrayList<Long> list = new ArrayList<>();
            for (TenantDiscountMemberStation tenantDiscountMemberStation : tenantDiscountMemberStations) {
                list.add(tenantDiscountMemberStation.getDiscountMemberStationId());
            }
            tenantDiscountMemberStationService.deleteByBatch(list);
        }

        //运营商优惠会员对象
        TenantDiscountMember tenantDiscountMember = new TenantDiscountMember();
        tenantDiscountMember.setDiscountPolicyId(tenantDiscountPolicy.getDiscountPolicyId());
        //会员类型(0个人用户 1企业用户 2全部用户) 默认0
        tenantDiscountMember.setMemberType("0");

        //会员中间表对象
        TenantDiscountMemberStation tenantDiscountMemberStation = new TenantDiscountMemberStation();
        tenantDiscountMemberStation.setTenantCode(tenantCode);
        tenantDiscountMemberStation.setDiscountPolicyId(discountPolicyId);
        tenantDiscountMemberStation.setElecStationId(tenantDiscountPolicy.getElecStationId());
        //失效状态 0有效 1过期
        tenantDiscountMemberStation.setStatus("0");

        int errorMemberCount = 0;
        int totalMemberCount = 0;

        if ("1".equals(memberType)) {
            String[] split = stringList.split("\n");
            for (String s : split) {
                totalMemberCount++;
                MemberInfo memberInfo = memberInfoService.selectMemberByMobile(tenantCode, s);
                if (memberInfo != null) {
                    tenantDiscountMember.setMemberId(memberInfo.getMemberId());
                    tenantDiscountMemberMapper.insertSelective(tenantDiscountMember);
                    tenantDiscountMemberStation.setMemberId(memberInfo.getMemberId());
                    tenantDiscountMemberStationService.insertSelective(tenantDiscountMemberStation);
                } else {
                    //失败的会员数
                    errorMemberCount++;
                }
            }

            //优惠用户类型 0 全部 1指定用户
            tenantDiscountPolicy.setPolicyMemberType("1");
            tenantDiscountPolicyService.updateByPrimaryKeySelective(tenantDiscountPolicy);
        } else {
            tenantDiscountPolicy.setPolicyMemberType("0");
            tenantDiscountPolicyService.updateByPrimaryKeySelective(tenantDiscountPolicy);
        }

        if (errorMemberCount == 0) {
            return null;
        } else if (errorMemberCount == totalMemberCount) {
            return new TResponse(500, "所有手机号都不是会员,请重新添加");
        } else {
            return new TResponse(200, "有" + errorMemberCount + "个手机号码不是会员");
        }
    }

    @Override
    public List<TenantDiscountMember> selectMemberByDiscountId(Long discountId) {
        TenantDiscountMemberExample example = new TenantDiscountMemberExample();
        example.createCriteria().andDiscountPolicyIdEqualTo(discountId);
        List<TenantDiscountMember> tenantDiscountMembers = tenantDiscountMemberMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tenantDiscountMembers)) {
            return tenantDiscountMembers;
        }
        return null;
    }

    @Override
    public void deleteByBatch(List<Long> discountMemberIds) {
        tenantDiscountMemberMapper.deleteByBatch(discountMemberIds);
    }


}
